package cultivameCore.color;

public class ColorRGB {
	
	private int red=0;
	private int blue=0;
	private int green=0;

	private static float GAMMA = 0.80f;
	private static float INTENSITYMAX = 255;
	
	public ColorRGB(){
		red=0;
		green=0;
		blue=0;
	}
	public ColorRGB(int _red, int _green,int _blue){
		red=_red;
		green=_green;
		blue=_blue;
	}

	public int getRed() {
		return red;
	}
	public void setRed(int red) {
		this.red = red;
	}
	public int getBlue() {
		return blue;
	}
	public void setBlue(int blue) {
		this.blue = blue;
	}
	public int getGreen() {
		return green;
	}
	public void setGreen(int green) {
		this.green = green;
	}
	
	private static int adjust(double color, double factor) {
		if (color == 0.0) {
			return 0; // Don't want 0^x = 1 for x <> 0
		} else {
			return (int) cultivameCore.utils.Math.round((float)(INTENSITYMAX * (double)cultivameCore.utils.Math.pow((float) (color * factor), GAMMA)));
		}
	}

	public static ColorRGB lonOndaToRGB(float nm) {
		float blue;
		float factor;
		float green;
		float red;
		if (nm >= 380 && nm <= 439) {
			red = -(nm - 440) / (440 - 380);
			green = 0.0f;
			blue = 1.0f;
		} else if (nm >= 340 && nm <= 489) {
			red = 0.0f;
			green = (nm - 440) / (490 - 440);
			blue = 1.0f;
		} else if (nm >= 490 && nm <= 509) {
			red = 0.0f;
			green = 1.0f;
			blue = -(nm - 510) / (510 - 490);
		} else if (nm >= 510 && nm <= 579) {
			red = (nm - 510) / (580 - 510);
			green = 1.0f;
			blue = 0.0f;
		} else if (nm >= 580 && nm <= 644) {
			red = 1.0f;
			green = -(nm - 645) / (645 - 580);
			blue = 0.0f;
		} else if (nm >= 645 && nm <= 780) {
			red = 1.0f;
			green = 0.0f;
			blue = 0.0f;
		} else {
			red = 0.0f;
			green = 0.0f;
			blue = 0.0f;
		}
		// Let the intensity fall off near the vision limits
		if (nm >= 380 && nm <= 419) {
			factor = 0.3f + 0.7f * (nm - 380) / (420 - 380);
		} else if (nm >= 420 && nm <= 700) {
			factor = 1.0f;
		} else if (nm >= 701 && nm <= 780) {
			factor = 0.3f + 0.7f * (780 - nm) / (780 - 700);
		} else {
			factor = 0.0f;
		}
		int r = adjust(red, factor);
		int g = adjust(green, factor);
		int b = adjust(blue, factor);
		return new ColorRGB(r, g, b);
	}
}
